#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>



int main()
{
	int fd[2] = { 0 };
	pipe(fd);
	pid_t id = fork();
	//子进程写父进程读
	if(id == 0)
	{
		//child
		//关闭读fd
		close(fd[0]);
		int cnt = 5;
		const char* str = "hello world";
		int count = 200;
		// while(count--)
		// 	sleep(1);
		while(count--)
		{
			write(fd[1], str, strlen(str));
			sleep(1);
		}
		exit(1);
		printf("child quit...\n");
	}
	//father
	//关闭写fd
	close(fd[1]);
	char buff[1024];

	int cnt = 5;
	while(cnt--)
	{
		ssize_t n = read(fd[0], buff, sizeof(buff) - 1);
		if(n > 0)
		{
			buff[n] = 0;
			printf("child say # %s\n", buff);
		}
		else if(n == 0)
		{
			printf("child quit, read all done, me too!\n");
			break;
		}
		else
		{
			printf("read error\n");
			break;
		}
	}
	exit(1);
	wait(NULL);
	return 0;
}